//
//  NewFeatureView.m
//  NewFeatureGuid
//
//  Created by gome on 2018/12/12.
//  Copyright © 2018 gome. All rights reserved.
//

#import "NewFeatureView.h"

@interface NewFeatureView ()
@property (nonatomic, strong) UIImageView *tipsView;
@property (nonatomic, strong) void (^compelete)(void);
@property (nonatomic, strong) CAShapeLayer *maskLayer;
@property (nonatomic, assign) NSInteger tipsIndex;
@property (nonatomic, strong) NSArray<NSDictionary *> *tipsArray;
@end

@implementation NewFeatureView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.7]];
        [self tipsView];
    }
    
    return self;
}


/**
 展示tips 的imageView

 @return imageView
 */
- (UIImageView *)tipsView {
    if (!_tipsView) {
        _tipsView = [[UIImageView alloc] init];
        [self addSubview:_tipsView];
        [self addGestureRecognizerToView:_tipsView];
    }
    
    return _tipsView;
}


/**
 外部调用设置方法

 @param array tips 数组
 @param compelete 结束回调
 */
- (void)setWithArray:(NSArray <NSDictionary *>*)array compelete:(void(^)(void))compelete{
    
    self.tipsIndex = 0;
    _tipsArray = array;
    _compelete = compelete;
    
    [self setSignTipsWithIndex:self.tipsIndex];
}


/**
 根据index 设置 tips

 @param index tips 的 下标
 */
- (void)setSignTipsWithIndex:(NSInteger)index {
    if (index >= _tipsArray.count) {
        self.compelete();
        return;
    }
    
    NSDictionary *dict = [_tipsArray objectAtIndex:index];
    if (![dict isKindOfClass:[NSDictionary class]]) {
        self.compelete();
        return;
    }
    
    NSString *maskViewRectString = [dict objectForKey:@"maskViewRect"];
    CGRect maskViewRect = CGRectFromString(maskViewRectString);
    NSString *tipsImageName = [dict objectForKey:@"tipsImageName"];
    [self setTipsViewWithFrame:maskViewRect withImage:tipsImageName];
    self.tipsIndex += 1;
}

/**
 设置 tips 的 frame

 @param frame 镂空区域 rect
 @param imageName tips 图片名称
 */
- (void)setTipsViewWithFrame:(CGRect)frame withImage:(nonnull NSString *)imageName {
    [self makeMaskLayerWithFrame:frame withView:self];
    
    if (_tipsView && imageName) {
        [_tipsView setImage:[UIImage imageNamed:imageName]];
        CGRect tipsViewRect = [self getRectWithFrame:frame];
        [_tipsView setFrame:tipsViewRect];
        [_tipsView setBackgroundColor:[UIColor yellowColor]];
    }
}

/**
 获取tipsImageView 的 frame

 @param frame 镂空k区域的frame
 @return 返回tipsImageView 的 frame
 */
- (CGRect)getRectWithFrame:(CGRect)frame {
    CGRect rect = CGRectMake((self.frame.size.width - frame.size.width) / 2.0f, 20.0, 100, 44);
    
    return rect;
}

/**
 给view添加手势

 @param view 添加手势的view
 */
- (void)addGestureRecognizerToView:(UIView *)view {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [view addGestureRecognizer:tap];
    [view setUserInteractionEnabled:YES];
    [tap addTarget:self action:@selector(tapView:)];
}


/**
 手势时间处理方法

 @param gester 手势对象
 */
- (void)tapView:(UITapGestureRecognizer *)gester {
    [self.maskLayer removeFromSuperlayer];
    self.maskLayer = nil;

    [self setSignTipsWithIndex:self.tipsIndex];
}


/**
 创建镂空layer

 @param frame 目标view相对根view的frame
 @param view layer 的s宿主view
 */
- (void)makeMaskLayerWithFrame:(CGRect)frame withView:(UIView *)view {
    
    // 第一个路径
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:[[UIScreen mainScreen] bounds]];
    [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:frame cornerRadius:5] bezierPathByReversingPath]];

    // 绘制透明区域
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    [view.layer setMask:shapeLayer];
    _maskLayer = shapeLayer;
}
@end
